package com.like.miaosha.service;

import com.like.miaosha.dao.MiaoshaUserDao;
import com.like.miaosha.domain.MiaoshaUser;
import com.like.miaosha.exception.GlobalException;
import com.like.miaosha.redis.MiaoshaUserKey;
import com.like.miaosha.redis.RedisService;
import com.like.miaosha.result.CodeMsg;
import com.like.miaosha.util.MD5Util;
import com.like.miaosha.util.UUIDUtil;
import com.like.miaosha.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Service
public class MiaoshaUserService {
    public static final String COOKIE_NAME_TOKEN="token";
    @Autowired
    private MiaoshaUserDao miaoshaUserDao;
    @Autowired
    private RedisService redisService;
    public MiaoshaUser getById(Long id){
        MiaoshaUser miaoshaUser = redisService.get(MiaoshaUserKey.getById, "" + id, MiaoshaUser.class);
        if(miaoshaUser!=null) return miaoshaUser;
        miaoshaUser=miaoshaUserDao.getById(id);
        if(miaoshaUser!=null){
            redisService.set(MiaoshaUserKey.getById,""+id,miaoshaUser);
        }
        return miaoshaUser;
    }
    public boolean updatePassword(String token,long id,String formPass){
        MiaoshaUser miaoshaUser = getById(id);
        if(miaoshaUser==null){
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }

        MiaoshaUser toBeUpdate=new MiaoshaUser();

        //更新数据库
        toBeUpdate.setId(id);
        toBeUpdate.setPassword(MD5Util.formPassToDBPass(formPass, miaoshaUser.getSalt()));
        miaoshaUserDao.update(toBeUpdate);

        //处理缓存
        miaoshaUser.setPassword(toBeUpdate.getPassword());
        redisService.set(MiaoshaUserKey.token,token,miaoshaUser);
        redisService.delete(MiaoshaUserKey.getById,""+id);
        return true;
    }

    public String login(HttpServletResponse resp, LoginVo loginVo){
        if(loginVo==null) throw new GlobalException(CodeMsg.SERVER_ERROR);
        String formPass=loginVo.getPassword();
        String mobile=loginVo.getMobile();
        MiaoshaUser user=getById(Long.valueOf(mobile));
        if(user==null) throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        //验证密码
        String dbPass=user.getPassword();
        String saltDB=user.getSalt();
        String calPass= MD5Util.formPassToDBPass(formPass,saltDB);
        if(!calPass.equals(dbPass)) throw new GlobalException(CodeMsg.PASS_WORD_ERROR);
        String token= UUIDUtil.uuid();
        addCookie(resp,token,user);
        return token;
    }
    public void addCookie(HttpServletResponse resp, String token, MiaoshaUser user){

        redisService.set(MiaoshaUserKey.token,token,user);
        Cookie cookie=new Cookie(COOKIE_NAME_TOKEN,token);
        cookie.setMaxAge(MiaoshaUserKey.token.expireSeconds());
        cookie.setPath("/");
        resp.addCookie(cookie);
    }
    public MiaoshaUser getByToken(HttpServletResponse resp, String token) {
        if(StringUtils.isEmpty(token)) return null;
        MiaoshaUser user= redisService.get(MiaoshaUserKey.token,token, MiaoshaUser.class);
        if(user!=null)
        addCookie(resp,token,user);
        return user;
    }
}
